<?php

class Verzoek {

    private static $_dit = null;
    private $_pad = ['module' => 'Inhoud',
        'controller' => 'Index',
        'actie' => 'index',
        'id' => 0,
        'alias' => ''];

    public static function VERWERK() {
        $tmp = rtrim(filter_input(INPUT_GET, 'pad'), '/');
        if (empty($tmp)) {
            self::STANDAARD();
        } else {
            $items = explode('/', $tmp);
            if (count($items) == 1) {
                self::$_dit->_pad['alias'] = $items[0];
                self::VERWERKALIAS();
            } elseif (count($items) >= 4) {
                self::$_dit->_pad['module'] = $items[0];
                self::$_dit->_pad['controller'] = $items[1];
                self::$_dit->_pad['actie'] = $items[2];
                self::$_dit->_pad['id'] = $items[3];
                self::OPEN();
            } else {
                self::STANDAARD();
            }
        }
    }

    public static function STANDAARD() {
        $id = Instelling::GET('website.standaard.frontend');
        self::$_dit->_pad['module'] = 'Inhoud';
        self::$_dit->_pad['controller'] = 'Index';
        self::$_dit->_pad['actie'] = 'index';
        self::$_dit->_pad['id'] = $id;
        self::OPEN();
    }

    public static function VERWERKALIAS() {
        $shortcuts = \Instelling::GET('website.shortcut');
        if (array_key_exists(strtolower(self::$_dit->_pad['alias']), $shortcuts)) {
            $shortcut = $shortcuts[strtolower(self::$_dit->_pad['alias'])];
            $shortcut = explode('/',$shortcut);
            self::$_dit->_pad['module'] = $shortcut[0];
            self::$_dit->_pad['controller'] = $shortcut[1];
            self::$_dit->_pad['actie'] = $shortcut[2];
            self::$_dit->_pad['id'] = $shortcut[3];
            self::OPEN();
        } else {
            $dbVerbinding = \Db::GET();
            $dbSql = "SELECT id FROM inhoud WHERE menualias=:alias";
            try {
                $dbStmt = $dbVerbinding->prepare($dbSql);
                $dbStmt->execute([':alias' => self::$_dit->_pad['alias']]);
                if ($dbStmt->rowCount() == 0) {
                    self::STANDAARD();
                } else {
                    self::$_dit->_pad['module'] = 'Inhoud';
                    self::$_dit->_pad['controller'] = 'Index';
                    self::$_dit->_pad['actie'] = 'index';
                    $dbRij = $dbStmt->fetch(\PDO::FETCH_ASSOC);
                    self::$_dit->_pad['id'] = $dbRij['id'];
                    self::OPEN();
                }
            } catch (PDOException $ex) {
                
            }
        }
    }

    public static function OPEN() {
        $klasse = implode('\\', [self::$_dit->_pad['module'], 'Controller', self::$_dit->_pad['controller']]);
        if (__klasse($klasse)) {
            $instantie = new $klasse();
            if (method_exists($instantie, self::$_dit->_pad['actie'])) {
                $actie = self::$_dit->_pad['actie'];
                $instantie->$actie();
            } else {
                self::STANDAARD();
            }
        } else {
            self::STANDAARD();
        }
    }

    public static function GETPADITEM($sleutel = '') {
        $sleutel = strtolower(trim($sleutel));
        if (!empty($sleutel)) {
            if (array_key_exists($sleutel, self::$_dit->_pad)) {
                return self::$_dit->_pad[$sleutel];
            } else {
                return false;
            }
        }
    }

    public static function SETPADITEM($sleutel = '', $waarde = '') {
        $sleutel = strtolower(trim($sleutel));
        if (!empty($sleutel)) {
            self::$_dit->_pad[$sleutel] = $waarde;
        }
    }

    public static function NIEUW() {
        header('location:' . \instelling::GET('website.root'));
    }

}
